package com.example.zengxianghui900.netcomponent;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.netcomponent.net.AppConstant;
import com.example.netcomponent.net.HttpManager;
import com.example.netcomponent.net.HttpOnNextListener;
import com.example.netcomponent.net.download.HttpDownOnNextListener;
import com.example.netcomponent.net.download.HttpDownloadManager;
import com.example.netcomponent.net.utils.FileUtils;
import com.example.netcomponent.net.utils.ToastUtils;
import com.example.zengxianghui900.netcomponent.api.CheckVersionApi;
import com.example.zengxianghui900.netcomponent.vo.VersionDataVo;
import com.google.gson.Gson;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.io.File;

public class MainActivity extends RxAppCompatActivity {

    private TextView mTvCheckVersion;
    private String mDownloadURL;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mTvCheckVersion = (TextView) findViewById(R.id.tv_check_version);
    }


    /**
     * 检查版本更新
     * @param view
     */
    public void checkVersion(View view){
        CheckVersionApi checkVersionApi = new CheckVersionApi(this);
        checkVersionApi.setProgressMsg("正在检查版本更新");
        checkVersionApi.setAppId(AppConstant.APP_ID);
        checkVersionApi.setListener(new HttpOnNextListener<VersionDataVo>() {
            @Override
            public void onNext(VersionDataVo versionDataVo) {
                mTvCheckVersion.setText(new Gson().toJson(versionDataVo));
                mDownloadURL = versionDataVo.getDownloadURL();
                ToastUtils.showShortToast(MainActivity.this,"检查版本更新成功");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ToastUtils.showShortToast(MainActivity.this,"检查版本更新失败");
            }
        });

        HttpManager.getInstance().doHttp(checkVersionApi);

    }

    /**
     * 下载apk
     * @param view
     */
    public void downloadApk(View view){
        if (TextUtils.isEmpty(mDownloadURL)){
            ToastUtils.showShortToast(this,"请先检查版本更新");
            return;
        }
        File apkSaveFile = FileUtils.getApkSaveFile("example.apk");
        //开始下载apk
        HttpDownloadManager.getInstance().startDownload(this,mDownloadURL,apkSaveFile, new HttpDownOnNextListener() {
            @Override
            public void onNext(Object o) {

            }

            @Override
            public void onStart() {
                if (mProgressDialog == null){
                    mProgressDialog = new ProgressDialog(MainActivity.this);
                }
                mProgressDialog.dismiss();  //先关闭
                mProgressDialog.setProgress(0);
                mProgressDialog.setTitle("apk下载");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setProgressNumberFormat("");
                mProgressDialog.show();
            }

            @Override
            public void onComplete() {
                mProgressDialog.dismiss();
                mProgressDialog.setProgress(0);
                ToastUtils.showShortToast(MainActivity.this,"apk下载成功");
            }

            @Override
            public void updateProgress(long readLength, long countLength) {
                mProgressDialog.setMax((int) countLength);
                mProgressDialog.setProgress((int) readLength);
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.showShortToast(MainActivity.this,"apk下载失败,请重试");
                mProgressDialog.dismiss();
                mProgressDialog.setProgress(0);
                super.onError(e);
            }
        });

    }
}
